package com.next

class QuestionnaireInstance
{

    String name
    QuestionnaireType type
    String description
    User createBy
    User modifyBy
    Date createdDate = new Date()
    Date modifiedDate = new Date()

    static belongsTo = [contact: Contact, questionnaire: Questionnaire, opportunity: Opportunity]

    SortedSet questions
    static hasMany = [questions: QuestionInstance]

    static constraints = {
        name maxSize: 64
        description nullable: true, blank: true, maxSize: 64
        createBy nullable: true, blank: true
        modifyBy nullable: true, blank: true
        opportunity nullable: true, blank: true
        contact nullable: true, blank: true
    }

    static mapping = {
        questions(lazy: false)
    }

    def springSecurityService

    def beforeInsert()
    {
        def username = springSecurityService?.getPrincipal()?.username
        createBy = User.findByUsername(username)
    }

    def beforeUpdate()
    {
        def username = springSecurityService?.getPrincipal()?.username
        modifyBy = User.findByUsername(username)
        modifiedDate = new Date()
    }
}
